﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2C.Framework.BaseUtility.Helper
{
    public  class FFmpegHelper
    {
        /// <summary>
        ///修复字幕
        /// </summary>
        /// <param name="videoPath">视频路径</param>
        /// <returns>无字幕视频文件路径</returns>
        public static string RepairVideo(string videoPath)
        {
            string newVideoPath = Path.Combine(Path.GetDirectoryName(videoPath), "修复_" + Path.GetFileName(videoPath));
            if (File.Exists(newVideoPath))
            {
                return newVideoPath;
            }
            //可以根据需要修改命令中的参数
            //完整命令：ffmpeg -f dshow -s 320*180 -i video="HP HD Camera" -f dshow -i audio="麦克风 (USB Microphone)" -vcodec mpeg4 -acodec aac -strict -2 myVideo.mp4
            //string command = "ffmpeg -i "+ videoPath + " -vcodec copy -acodec copy -sn "+ newVideoPath;
            string command = $"-i {videoPath} -vcodec copy -an {newVideoPath}";
             RunCmd(command);
            return newVideoPath;
        }

        public static string VideoConverter(string videoPath)
        {
            string newVideoPath = Path.Combine(Path.GetDirectoryName(videoPath), "校验_" + Path.GetFileNameWithoutExtension(videoPath) + ".mp4");
            if (File.Exists(newVideoPath))
            {
                return newVideoPath;
            }
            //可以根据需要修改命令中的参数
            //完整命令：ffmpeg -f dshow -s 320*180 -i video="HP HD Camera" -f dshow -i audio="麦克风 (USB Microphone)" -vcodec mpeg4 -acodec aac -strict -2 myVideo.mp4
            //string command = "ffmpeg -i "+ videoPath + " -vcodec copy -acodec copy -sn "+ newVideoPath;
            string command = $"-i {videoPath} -c:v libx264 -preset ultrafast -crf 18 -c:a aac {newVideoPath}";
            RunCmd(command);
            return newVideoPath;
        }
        /// <summary>
        /// 拼接视频
        /// </summary>
        /// <param name="videoPath"></param>
        /// <returns></returns>
        public static string SplicingVideo(List<FileInfo> videoPaths)
        {
            string newVideoPath = Path.Combine(Path.GetDirectoryName(videoPaths.FirstOrDefault().FullName), "合成_" + Path.GetFileName(videoPaths.FirstOrDefault().FullName));
            if (File.Exists(newVideoPath))
            {
                return newVideoPath;
            }
            var sw= File.CreateText(Path.Combine(Path.GetDirectoryName(videoPaths.FirstOrDefault().FullName), "filelist.txt"));
            foreach (var item in videoPaths)
            {
                sw.WriteLine($"file '{item.FullName}'");
            }
            sw.Flush();
            sw.Close();
            sw.Dispose();
            string command = $"-f concat -safe 0 -y -i {Path.Combine(Path.GetDirectoryName(videoPaths.FirstOrDefault().FullName), "filelist.txt")} -c copy -strict -2 {newVideoPath}";
            RunCmd(command);
            return newVideoPath;
        }
        public static string RunCmd(string cmd )
        {
            string FFmpegPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ffmpeg.exe");
            Process p = new Process();
            p.StartInfo.FileName = FFmpegPath;//ffmpeg.exe路径
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.Arguments = cmd;
            p.StartInfo.UseShellExecute = false;//不使用系统外壳程序启动进程
            p.StartInfo.CreateNoWindow = true;  //不显示dos程序窗口
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中
            p.StartInfo.UseShellExecute = false;
            p.Start();
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            p.BeginErrorReadLine();//开始异步读取
            p.WaitForExit();//阻塞等待进程结束
            p.Close();//关闭进程
            p.Dispose();//释放资源
            return "";
        }
    }
}
